package com.zsxb.crm.controller;

import com.zsxb.crm.annotation.Log;
import com.zsxb.crm.dto.DeptDTO;
import com.zsxb.crm.pojo.Dept;
import com.zsxb.crm.result.Result;
import com.zsxb.crm.service.DeptService;


import com.zsxb.crm.vo.TreeselectVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/system/dept")
@Slf4j
public class DeptController {
    @Autowired
    private DeptService deptService;
    //查询部门
    @GetMapping("/list")
     public Result list(DeptDTO deptDTO){
         log.info("查询部门{}",deptDTO);
        List<Dept> deptList = deptService.getDeptList(deptDTO);
        return Result.success(deptList);
     }
     //添加部门
    @PostMapping
    @Log
    public Result add(@RequestBody DeptDTO deptDTO){
        log.info("添加部门{}",deptDTO);
        deptService.insert(deptDTO);
        return Result.success();
    }
    //根据id查询部门
    @GetMapping("/{getById}")
     public Result getById(@PathVariable Long getById){
         log.info("根据id查询部门{}",getById);
         Dept dept = deptService.excludeById(getById);
         return Result.success(dept);
     }
    //根据id查询部门
    @GetMapping("/list/exclude/{getById}")
    public Result getById2(@PathVariable Long getById){
        log.info("根据id查询部门{}",getById);
        Dept dept = deptService.excludeById(getById);
        return Result.success(dept);
    }
     //修改部门
    @PutMapping
    @Log
    public Result update(@RequestBody DeptDTO deptDTO){
        log.info("修改部门{}",deptDTO);
        deptService.update(deptDTO);
        return Result.success();
    }
    //删除部门
    @DeleteMapping("/{id}")
    @Log
    public Result delete(@PathVariable Long id){
        log.info("删除部门{}",id);
        deptService.delete(id);

        return Result.success();
    }

    @GetMapping("/roleDeptTreeselect/{roleId}")
    public Result roleDeptTreeselect(@PathVariable Integer roleId){
        log.info("根据角色id查询部门{}",roleId);
        List<TreeselectVO> roleDeptTreeselect = deptService.roleDeptTreeselect(roleId);
        return Result.success(roleDeptTreeselect);
    }

    @GetMapping("/treeselect")
    public Result treeselect(){
        List<TreeselectVO> deptList = deptService.list();
        return Result.success(deptList);
    }
}
